﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.IO;

namespace MetroTube.UI
{
    class Utility
    {

        public static string FileNameValidString(string s)
        {
            string fileName = s;

            if (fileName.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
                fileName = Regex.Replace(fileName, @"[\/?:*""><|]+", "", RegexOptions.Compiled);

            return fileName;
        }

        public static int FFmpegAudioConvert(string input, string output, string bitrate)
        {
            Process proc = new Process();

            proc.StartInfo.FileName = "ffmpeg";
            proc.StartInfo.Arguments = String.Format("-i \"{0}\" -vn -ab {1} \"{2}\"", input, bitrate, output);
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.UseShellExecute = false;

            proc.StartInfo.CreateNoWindow = true;


            if (!proc.Start())
            {
                Console.WriteLine("Converting error!");
                return -1;
            }

            StreamReader reader = proc.StandardError;

            string line;


            while ((line = reader.ReadLine()) != null)
                Console.WriteLine(line);

            proc.Close();

            return 1;
        }

    }
}
